﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using zjx.Wpf.Graffitiboard.Core;

namespace zjx.Wpf.Graffitiboard.Adorners
{
    public class ResizeRotateAdorner : Adorner
    {
        private ResizeRotateChrome chrome;

        private ISelectable selectedItem;

        private VisualCollection visualChildren;

        public ResizeRotateAdorner(UIElement adornedElement, ISelectable item)
              : base(adornedElement)
        {
            this.selectedItem = item;
            this.chrome = new ResizeRotateChrome();
            this.chrome.IsHitTestVisible = true;
            this.chrome.DataContext = item;
            visualChildren = new VisualCollection(this);
            visualChildren.Add(this.chrome);

            this.IsHitTestVisibleChanged += this.ResizeRotateAdorner_IsHitTestVisibleChanged;
        }

        #region 事件处理
        private void ResizeRotateAdorner_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.chrome.IsHitTestVisible = (bool)e.NewValue;
        }
        #endregion

        #region override
        protected override Size ArrangeOverride(Size finalSize)
        {
            Size size = base.ArrangeOverride(finalSize);
            Rect rect = this.selectedItem.GetBounds();
            if (this.chrome != null)
                this.chrome.Arrange(rect);

            return finalSize;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Rect rect = this.selectedItem.GetBounds();
            return rect.Size;
        }

        // Override the VisualChildrenCount and 
        // GetVisualChild properties to interface with 
        // the adorner's visual collection.
        protected override int VisualChildrenCount
        {
            get { return visualChildren.Count; }
        }

        protected override Visual GetVisualChild(int index)
        {
            return visualChildren[index];
        }
        #endregion
    }
}
